import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { DictApi, OutsourceIssue } from '#/api';

import { reactive, ref } from 'vue';

import { DictResp, getOutsourceIssueCode } from '#/api';

const status = reactive<DictApi.SelectOption[]>([]);
DictResp(['mes_order_status']).then((res) => {
  if (res.mes_order_status) {
    status.push(...res.mes_order_status);
  }
});

export const mes_order_status = status;

const switchValue = ref('');
/**
 * 获取编辑表单的字段配置
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'issueId',
      dependencies: {
        triggerFields: ['issueId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'issueCode',
      label: '外协发料单编码',
      rules: 'required',
      formItemClass: 'col-span-5',
      labelWidth: 120,
      componentProps: {
        placeholder: '请输入外协发料单编码',
      },
      dependencies: {
        triggerFields: ['switch'],
        trigger(values) {
          values.issueCode = switchValue;
        },
      },
    },
    {
      component: 'Switch',
      fieldName: 'switch',
      label: '自动生成',
      formItemClass: 'col-span-2 col-start-6',
      labelWidth: 70,
      componentProps: {
        defaultValue: 0,
        onChange: (e: boolean) => {
          if (e) {
            getOutsourceIssueCode().then((res) => {
              switchValue.value = res.data;
            });
          } else {
            switchValue.value = '';
          }
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'issueName',
      label: '外协发料单名称',
      labelWidth: 120,
      rules: 'required',
      componentProps: {
        placeholder: '请输入外协发料单名称',
      },
      formItemClass: 'col-start-8 col-span-5',
    },
    {
      component: 'DatePicker',
      fieldName: 'issueDate',
      label: '发料日期',
      formItemClass: 'col-span-4',
    },

    {
      component: 'Input',
      fieldName: 'charge',
      defaultValue: '(暂时不知道数据哪里来)',
      label: '发料仓库',
      formItemClass: 'col-span-4',
    },
    {
      component: 'Select',
      rules: 'required',
      componentProps: {
        allowClear: true,
        filterOption: true,
        options: mes_order_status,
        placeholder: '请选择单据状态',
        showSearch: true,
      },
      fieldName: 'status',
      label: '单据状态',
      formItemClass: 'col-span-4',
    },
    {
      component: 'Input',
      fieldName: 'workorderCode',
      label: '外协工单',
      formItemClass: 'col-span-4',
    },
    {
      component: 'Input',
      fieldName: 'vendorCode',
      label: '供应商',
      formItemClass: 'col-span-4',
    },
    {
      component: 'Input',
      fieldName: 'vendorName',
      label: '供应商名称',
      formItemClass: 'col-span-4',
    },

    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注信息',
        maxLength: 50,
        rows: 3,
        showCount: false,
      },
      fieldName: 'remark',
      label: '备注',
      emptyStateValue: null,
      formItemClass: 'col-span-12',
    },
  ];
}

/**
 * 获取表格列配置
 * @param onView 表格点击函数
 * @param _onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onView: (param: OutsourceIssue.Row) => void,
  _onActionClick?: OnActionClickFn<OutsourceIssue.Row>,
): VxeTableGridOptions<OutsourceIssue.Row>['columns'] {
  return [
    {
      field: 'checkbox',
      fixed: 'left',
      type: 'checkbox',
      width: 50,
    },
    {
      field: 'issueCode',
      fixed: 'left',
      title: '外协发料单编码',
      cellRender: { name: 'CellButtonInfo', props: { onClick: onView } },
      width: 150,
    },
    {
      field: 'issueName',
      title: '发料单名称',
      width: 150,
    },

    {
      field: 'workorderCode',
      title: '生产工单编码',
      width: 180,
    },
    {
      field: 'vendorName',
      title: '供应商名称',
    },
    {
      field: 'issueDate',
      title: '发料日期',
    },

    {
      field: 'status',
      title: '单据状态',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          mes_order_status.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    {
      field: 'operation',
      title: '操作',
      width: 200,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}
